package j.todo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class GCalendarUtil {
	final String CONTENT_URI = "content://com.android.calendar";	
	private int calendarId = -1;
	private ContentResolver cor = null;
	
	public GCalendarUtil(ContentResolver cor){		
		this.cor = cor;
		Uri uri = Uri.parse(CONTENT_URI + "/calendars");
		Cursor c = cor.query(
		             uri, new String[] { "_id" }, 
		             "selected=?", new String[] { "1" }, null);
		if(c == null || !c.moveToFirst()) {
			Log.i("ERROR", "There are no calendars");
		}
		calendarId = c.getInt(c.getColumnIndex("_id"));
	}
	/**
	 * 
	 * @param content todo list 내용 
	 * @param datetime yyyy-MM-dd 형식의 날
	 * @throws ParseException
	 */
	public String addEvent(String content, String datetime) throws ParseException{
		ContentValues cv = new ContentValues();
		cv.put("calendar_id", calendarId);
		cv.put("title", content);
		cv.put("description", content);		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date dtstart = format.parse(datetime);		
		cv.put("dtstart", dtstart.getTime());
		cv.put("dtend", dtstart.getTime());
		cv.put("duration", "P1D");
		cv.put("allDay", 1);		
		Uri result = cor.insert(Uri.parse(CONTENT_URI + "/events"), cv);
		return result.getLastPathSegment();
	}
	
	/**
	 * 
	 * @param eventId Google Calendar Event의 고유 id
	 */
	public void removeEvent(String eventId){
		Uri uri = Uri.parse(CONTENT_URI + "/events");
		// 이전 파트에서 집어 넣었던 일정을 검색한다.
		int cnt = cor.delete(
		             uri, // 가져올 필드
		             "calendar_id=? AND _id=?",
		             new String[] { String.valueOf(calendarId), eventId });
		if (cnt > 0){
			Log.i("INFO", "Google Calendar Event is deleted");
		}else{
			Log.i("ERROR", "Google Calendar Event is not deleted");
		}
	}
	
	/**
	 * 현재 시각 이후의 google calendar만 리
	 * @return
	 */
	public List<GCalendarModel> getEvents(){
		Uri uri = Uri.parse(CONTENT_URI + "/events");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		Cursor c = cor.query(
		             uri, new String[] { "_id", "dtstart", "title" }, // 가져올 필드
		             "calendar_id=? AND dtstart>=? AND deleted=0 ",
		             new String[] { String.valueOf(calendarId), String.valueOf(Calendar.getInstance().getTime().getTime()) }, null );
		
		List<GCalendarModel> results = new ArrayList<GCalendarModel>();
		  
		if (c.getCount() > 0) {
			while(c.moveToNext()) {
				String eventId = c.getString(c.getColumnIndex("_id"));				
				String content = c.getString(c.getColumnIndex("title"));  
				Date dtstart = new Date(c.getLong(c.getColumnIndex("dtstart")));		
				String datetime = format.format(dtstart);
				results.add(new GCalendarModel(eventId, content, datetime));
			}
		}
		return results;		
	}
	
}
